//
//  ViewController.m
//  PhotosBrowser
//
//  Created by winter on 2016/10/22.
//  Copyright © 2016年 JHJR. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "WPhotosBrowserView.h"

@interface PhotoCollectionViewCell : UICollectionViewCell
@property (nonatomic, copy) NSString *imageUrlString;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@interface PhotoTableViewCell : UITableViewCell
@property (nonatomic, copy) NSString *imageUrlString;
@property (weak, nonatomic) IBOutlet UIImageView *imgeView;
@end

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UITableViewDelegate, UITableViewDataSource, WPhotosBrowserViewDelegate, WPhotosBrowserViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *originalImageUrls;

@property (nonatomic, strong) WPhotosBrowserView *photosBrowserView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSMutableArray *thumbnailImageUrls = [NSMutableArray array];
    // 添加图片(缩略图)链接
    [thumbnailImageUrls addObject:@"http://ww3.sinaimg.cn/thumbnail/006ka0Iygw1f6bqm7zukpj30g60kzdi2.jpg"];
    [thumbnailImageUrls addObject:@"http://ww1.sinaimg.cn/thumbnail/61b69811gw1f6bqb1bfd2j20b4095dfy.jpg"];
    [thumbnailImageUrls addObject:@"http://ww1.sinaimg.cn/thumbnail/54477ddfgw1f6bqkbanqoj20ku0rsn4d.jpg"];
    [thumbnailImageUrls addObject:@"http://ww4.sinaimg.cn/thumbnail/006ka0Iygw1f6b8gpwr2tj30bc0bqmyz.jpg"];
    [thumbnailImageUrls addObject:@"http://ww2.sinaimg.cn/thumbnail/9c2b5f31jw1f6bqtinmpyj20dw0ae76e.jpg"];
    [thumbnailImageUrls addObject:@"http://ww1.sinaimg.cn/thumbnail/536e7093jw1f6bqdj3lpjj20va134ana.jpg"];
    [thumbnailImageUrls addObject:@"http://ww1.sinaimg.cn/thumbnail/75b1a75fjw1f6bqn35ij6j20ck0g8jtf.jpg"];
    [thumbnailImageUrls addObject:@"http://ww4.sinaimg.cn/bmiddle/406ef017jw1ec40av2nscj20ip4p0b29.jpg"];
    [thumbnailImageUrls addObject:@"http://ww1.sinaimg.cn/thumbnail/86afb21egw1f6bq3lq0itj20gg0c2myt.jpg"];
    
    // 1.2 创建图片原图链接数组
    NSMutableArray *originalImageUrls = [NSMutableArray array];
    // 添加图片(原图)链接
    [originalImageUrls addObject:@"http://ww3.sinaimg.cn/large/006ka0Iygw1f6bqm7zukpj30g60kzdi2.jpg"];
    [originalImageUrls addObject:@"http://ww1.sinaimg.cn/large/61b69811gw1f6bqb1bfd2j20b4095dfy.jpg"];
    [originalImageUrls addObject:@"http://ww1.sinaimg.cn/large/54477ddfgw1f6bqkbanqoj20ku0rsn4d.jpg"];
    [originalImageUrls addObject:@"http://ww4.sinaimg.cn/large/006ka0Iygw1f6b8gpwr2tj30bc0bqmyz.jpg"];
    [originalImageUrls addObject:@"http://ww2.sinaimg.cn/large/9c2b5f31jw1f6bqtinmpyj20dw0ae76e.jpg"];
    [originalImageUrls addObject:@"http://ww1.sinaimg.cn/large/536e7093jw1f6bqdj3lpjj20va134ana.jpg"];
    [originalImageUrls addObject:@"http://ww1.sinaimg.cn/large/75b1a75fjw1f6bqn35ij6j20ck0g8jtf.jpg"];
    [originalImageUrls addObject:@"http://ww4.sinaimg.cn/bmiddle/406ef017jw1ec40av2nscj20ip4p0b29.jpg"];
    [originalImageUrls addObject:@"http://ww1.sinaimg.cn/large/86afb21egw1f6bq3lq0itj20gg0c2myt.jpg"];
    
    self.originalImageUrls = [originalImageUrls copy];
    self.dataArray = [thumbnailImageUrls copy];
    [self.collectionView reloadData];
    [self.tableView reloadData];
    
    self.photosBrowserView = [WPhotosBrowserView photosBrowserView];
    self.photosBrowserView.dataSource = self;
    self.photosBrowserView.delegate = self;
}

#pragma mark - WPhotosBrowserViewDataSource

- (NSArray<NSString *> *)thumbnailUrlsForBrowserWithView:(WPhotosBrowserView *)photosBrowserView
{
    return self.dataArray;
}

- (NSArray<NSString *> *)imageUrlsForBrowserWithView:(WPhotosBrowserView *)photosBrowserView
{
    return self.dataArray;
}

- (UIView *)photosBrowserViewWillShowAtLocationViewWithIndex:(NSInteger)index
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    PhotoCollectionViewCell *cell = (PhotoCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    
    return cell.imageView;
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    cell.imageUrlString = self.dataArray[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    PhotoCollectionViewCell *cell = (PhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    [self.photosBrowserView showWithIndex:indexPath.item];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];
    cell.imageUrlString = self.dataArray[indexPath.item];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PhotoTableViewCell *cell = (PhotoTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"collectionViewCell = %@",cell);
}
@end

@implementation PhotoCollectionViewCell

- (void)setImageUrlString:(NSString *)imageUrlString
{
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlString]];
}

@end

@implementation PhotoTableViewCell

- (void)setImageUrlString:(NSString *)imageUrlString
{
    [self.imgeView sd_setImageWithURL:[NSURL URLWithString:imageUrlString]];
}

@end
